/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

/**
 *
 * @author Nam
 */
@Entity
public class AccessUser implements ICommonEntity, Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    private String password;

    private String name;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    @JoinTable(name = "AccessUser_AccessGroup", joinColumns = {
        @JoinColumn(name = "AccessUser", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "AccessGroup", referencedColumnName = "id")})
    private List<AccessGroup> accessGroups;

    public void addAccessGroup(AccessGroup group) {
        if (accessGroups == null) {
            accessGroups = new ArrayList<AccessGroup>();
            accessGroups.add(group);
            return;
        }
        if (!accessGroups.contains(group)) {
            accessGroups.add(group);
        }
    }

    public void addAllAccessGroup(Collection<AccessGroup> groups) {
        for (AccessGroup a : groups) {
            addAccessGroup(a);
        }
    }

    public void removeAccessGroup(AccessGroup group) {
        if (accessGroups == null) {
            return;
        }
        accessGroups.remove(group);
    }

    public List<AccessGroup> getAccessGroups() {
        if (accessGroups == null) {
            return new ArrayList<AccessGroup>();
        }
        return accessGroups;
    }

    public void setAccessGroups(List<AccessGroup> accessRole) {
        this.accessGroups = accessRole;
    }

    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof AccessUser)) {
            return false;
        }
        AccessUser other = (AccessUser) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.hutech09.jsale.entity.AccessUser[ id=" + id + " ]";
    }
}
